// controllers/userController.js
const User = require('../models/user');
const { findUser } = require('../services/userService')
const userController = {
    createUser: (req, res) => {
        const { username, password } = req.body;
        // 空值校验
        if (!username || !password) {
            return res.status(200).send({
                message: '用户名/密码为空',
                success: false,
            });
        }

        User.create(req.body, (error, results) => {
            if (error) {
                res.status(200).send({ message: error.message, success: false });
            } else {
                findUser(results.insertId, (error, results) => {
                    res.status(201).send({ data: results[0], message: '注册成功', success: true });
                })
            }
        });
    },
    deleteUser: (req, res) => {
        const { id } = req.body;
        if (!id) {
            return res.status(200).send({ message: '用户id为空', success: false });
        }

        User.delete(id, (error, results) => {
            if (error) {
                res.status(200).send({ message: error.message, success: false });
            } else if (results.affectedRows === 0) {
                res.status(200).send({ message: '用户不存在', success: false });
            } else {
                res.status(200).send({ message: '用户删除成功', success: true });
            }
        });
    },
    updateUser: (req, res) => {
        const { id, username, password, role } = req.body;
        // 空值校验
        if (!id || !username || !password) {
            return res.status(200).send({
                message: '用户id/账号/密码为空',
                success: false,
            });
        }

        User.update(id, { username, password, role }, (error, results) => {
            if (error) {
                res.status(200).send({ message: error.message, success: false });
            } else if (results.affectedRows === 0) {
                res.status(200).send({ message: '用户不存在', success: false });
            } else {
                res.status(200).send({ message: '用户更新成功', success: true });
            }
        });
    },
    login: (req, res) => {
        const { password, username } = req.body;
        if (!password || !username) {
            return res.status(200).send({ message: '用户/密码为空', success: false });
        }

        User.login({ password, username }, (error, results) => {
            if (error) {
                res.status(200).send({ message: error.message, success: false });
            } else if (results.length === 0) {
                res.status(200).send({ message: '用户不存在', success: false });
            } else {
                const user = results[0]
                if (user.role == 1) {
                    res
                    .status(200)
                        .send({ message: '角色错误', success: false, });
                    return;
                }
                res
                    .status(200)
                    .send({ message: '登录成功', success: true, data: results[0] });
            }
        });
    },

    clientLogin: (req, res) => {
        const { password, username } = req.body;
        if (!password || !username) {
            return res.status(200).send({ message: '用户/密码为空', success: false });
        }

        User.login({ password, username }, (error, results) => {
            if (error) {
                res.status(200).send({ message: error.message, success: false });
            } else if (results.length === 0) {
                res.status(200).send({ message: '用户不存在', success: false });
            } else {
                const user = results[0]
                if (user.role == 2) {
                    res
                    .status(200)
                        .send({ message: '角色错误', success: false, });
                    return;
                }
                res
                    .status(200)
                    .send({ message: '登录成功', success: true, data: results[0] });
            }
        });
    },
    getUserAll: (req, res) => {
        // 从请求中获取page和limit参数
        const page = parseInt(req.body.page, 10) || 1; // 默认为第1页
        const limit = parseInt(req.body.limit, 10) || 10; // 默认每页显示10条记录
        User.findAll(page, limit, (error, results) => {
            if (error) {
                res.status(200).send({ message: error.message, success: false });
            } else {
                User.find((error, users) => {
                    res
                        .status(200)
                        .send({ data: results, page: page, limit: limit, total: users.length, success: true });
                })
                // 返回分页数据

            }
        });
    },
};

module.exports = userController;